package cn.larry.common.utils;

/**
 * This is created,
 * but not necessarily updated by me.
 * Therefore, AUTHOR is useless.
 * 2024/8/11 13:02
 * <p>
 * TEST：密码生成的字典
 *
 * @Version 1.0
 */

import java.io.FileWriter;
import java.io.IOException;

public class PasswordDictionary {
//    public static void main(String[] args) {
//        run(8 ,"dictionary.txt");
//    }

    public static void run(int passwordLength, String outputFile) {
        char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+.?".toCharArray();

        try (FileWriter writer = new FileWriter(outputFile)) {
            generatePasswordCombinations(chars, passwordLength, new char[passwordLength], writer);
        } catch (IOException e) {
            System.out.println("Error writing to file: " + e.getMessage());
        }
    }

    private static void generatePasswordCombinations(char[] chars, int length, char[] currentCombination, FileWriter writer) throws IOException {
        if (currentCombination.length == length) {
            writer.write(new String(currentCombination) + System.lineSeparator());
            return;
        }

        for (char c : chars) {
            currentCombination[currentCombination.length - 1] = c;
            generatePasswordCombinations(chars, length, currentCombination, writer);
        }
    }
}
